package com.dong.shine.modules.common.controller;

import cn.hutool.core.io.IoUtil;
import com.dong.common.Result;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/file")
public class FileController {
    @GetMapping("/t.png")
    public void image(HttpServletResponse httpServletResponse)  {
        //这种方式在可以给页面返回网络资源。访问网络资源时，将网络资源转换为二进制流
        try(InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("static\\images\\0.jpg");) {
            if (null == resourceAsStream){
                return;
            }

            byte[] image = IoUtil.read(resourceAsStream).toByteArray();
            httpServletResponse.setContentType("image/png");
            try (OutputStream os = httpServletResponse.getOutputStream()) {
                os.write(image);
                os.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }catch (IOException ioException){
            ioException.printStackTrace();
        }

    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file){
        return Result.success();
    }
}
